package com.sbtr.business.auth.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.sbtr.util.PageDtoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.open.auth.dto.RoleAuthDto;
import com.sbtr.open.auth.dto.RoleDto;
import com.sbtr.business.auth.entity.Role;
import com.sbtr.business.auth.entity.RoleAuth;
import com.sbtr.business.auth.mapper.RoleMapper;
import com.sbtr.business.auth.service.IRoleAuthService;
import com.sbtr.business.auth.service.IRoleService;
import com.sbtr.common.PageDto;
import com.sbtr.util.BeanCopyUtils;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *

 */
@Transactional
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

	@Autowired
	private IRoleAuthService roleAuthService;
	
	@Override
	public RoleDto findById(Long id) {
		return BeanCopyUtils.copyProperties(getById(id), RoleDto.class);
	}

	@Override
	public boolean deleteById(Long id) {
		return removeById(id);
	}

	@Override
	public PageDto<RoleDto> search(String roleName, Long currPage, Long pageSize) {
		
		IPage<Role> page = new Page<>();
		if(currPage != null){
			page.setCurrent(currPage);
		}
		if(pageSize != null){
			page.setSize(pageSize);
		}
		
		QueryWrapper<Role> wrapper = new QueryWrapper<>();
		if(StringUtils.isNotEmpty(roleName)){
			wrapper.like("role_name", roleName);
		}
		wrapper.orderByDesc("id");
		
		page = page(page, wrapper);
		
		return PageDtoUtils.getPageDtoByIPage(page, RoleDto.class);
	}

	@Override
	public RoleDto save(RoleDto dto) {
		Role role = BeanCopyUtils.copyProperties(dto, Role.class);
		saveOrUpdate(role);
		dto.setId(role.getId());
		return dto;
	}

	@Override
	public boolean addAuth(Long roleId, List<String> userIds) {
		List<RoleAuth> authList = new ArrayList<>();
		for(String userId : userIds){
			RoleAuth auth = new RoleAuth();
			auth.setRoleId(roleId);
			auth.setUserId(userId);
			authList.add(auth);
		}
		deleteAuth(roleId, userIds);			//先删除重复授权的用户
		return roleAuthService.saveBatch(authList);	//再保存
	}

	@Override
	public boolean deleteAuth(Long roleId, List<String> userIds) {
		QueryWrapper<RoleAuth> wrapper = new QueryWrapper<>();
		wrapper.eq("role_id", roleId).in("user_id", userIds);
		return roleAuthService.remove(wrapper);
	}

	@Override
	public List<RoleAuthDto> getAuth(Long roleId) {
		QueryWrapper<RoleAuth> wrapper = new QueryWrapper<>();
		wrapper.eq("role_id", roleId);
		return BeanCopyUtils.copyProperties(roleAuthService.list(wrapper), RoleAuthDto.class);
	}

	@Override
	public boolean isAdmin(String userId) {
		return !StringUtils.isEmpty(userId) && baseMapper.isAdmin(userId);
	}

}
